<template>
  <div style="margin-top: 20px">
    <el-row>
      <el-col :span="23">
        <el-form ref="form" :model="form" label-width="100px">
          <el-form-item
            :rules="[
              { required: true, message: '不能为空'}
            ]"
            label="名称"
            prop="name">
            <el-input v-model="form.name"/>
          </el-form-item>
          <el-form-item
            :rules="[
              { required: true, message: '不能为空'}
            ]"
            label="描述"
            prop="content">
            <el-input
              :rows="2"
              v-model="form.content"
              type="textarea"
              placeholder="请输入内容"/>
          </el-form-item>
          <el-form-item
            :rules="[
              { required: true, message: '不能为空'}
            ]"
            label="图片"
            prop="imageUrl">
            <image-uploader :file-url.sync="form.imageUrl"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="save">保存</el-button>
          </el-form-item>

        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import productApi from '@/api/product/product'
import ImageUploader from '@/components/ImageUploader'
import WangEditor from '@/components/WangEditor/index'

export default {
  name: 'ProductEdit',
  components: {
    ImageUploader,
    WangEditor
  },
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 表单数据
      form: {
        id: this.id,
        name: '',
        content: '',
        imageUrl: ''
      }
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    getDetail() {
      productApi.detail({id: this.form.id}).then(response => {
        if (response.data.code === 200) {
          this.form = response.data.data
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    // 保存数据
    save() {
      // 验证表单数据
      this.$refs.form.validate((valid) => {
        if (valid) {
          productApi.edit(this.form).then(response => {
            if (response.data.code === 200) {
              this.$message.success('保存成功')
              this.pageOver()
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
